<template>
  <button
    class="custom-button"
    :class="[
      `btn-${type}`,
      `btn-${size}`,
      {
        'btn-disabled': disabled,
        'btn-loading': loading,
        'btn-block': block,
        'btn-round': round
      }
    ]"
    :disabled="disabled || loading"
    @click="handleClick"
  >
    <view v-if="loading" class="btn-loading-icon">
      <view class="loading-spinner"></view>
    </view>
    <view v-else-if="icon" class="btn-icon">
      <text class="iconfont" :class="icon"></text>
    </view>
    <text v-if="$slots.default || text" class="btn-text">
      <slot>{{ text }}</slot>
    </text>
  </button>
</template>

<script setup lang="ts">
interface Props {
  type?: 'primary' | 'secondary' | 'success' | 'warning' | 'error' | 'outline' | 'ghost';
  size?: 'sm' | 'md' | 'lg';
  text?: string;
  icon?: string;
  disabled?: boolean;
  loading?: boolean;
  block?: boolean;
  round?: boolean;
}

interface Emits {
  (e: 'click', event: Event): void;
}

const props = withDefaults(defineProps<Props>(), {
  type: 'primary',
  size: 'md',
  text: '',
  icon: '',
  disabled: false,
  loading: false,
  block: false,
  round: false
});

const emit = defineEmits<Emits>();

const handleClick = (event: Event) => {
  if (!props.disabled && !props.loading) {
    emit('click', event);
  }
};
</script>

<style lang="scss" scoped>
.custom-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--spacing-xs);
  padding: var(--spacing-sm) var(--spacing-md);
  border: none;
  border-radius: var(--border-radius-md);
  font-size: 14px;
  font-weight: 500;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
  transition: all 0.2s ease;
  user-select: none;
  outline: none;
  position: relative;
  overflow: hidden;
  
  &:active {
    transform: scale(0.98);
  }
}

// 按钮类型样式
.btn-primary {
  background-color: var(--color-primary);
  color: white;
  
  &:hover:not(.btn-disabled) {
    background-color: var(--color-primary-dark);
  }
}

.btn-secondary {
  background-color: var(--color-secondary);
  color: white;
  
  &:hover:not(.btn-disabled) {
    opacity: 0.8;
  }
}

.btn-success {
  background-color: var(--color-success);
  color: white;
  
  &:hover:not(.btn-disabled) {
    opacity: 0.8;
  }
}

.btn-warning {
  background-color: var(--color-warning);
  color: white;
  
  &:hover:not(.btn-disabled) {
    opacity: 0.8;
  }
}

.btn-error {
  background-color: var(--color-error);
  color: white;
  
  &:hover:not(.btn-disabled) {
    opacity: 0.8;
  }
}

.btn-outline {
  background-color: transparent;
  color: var(--color-primary);
  border: 1px solid var(--color-primary);
  
  &:hover:not(.btn-disabled) {
    background-color: var(--color-primary);
    color: white;
  }
}

.btn-ghost {
  background-color: transparent;
  color: var(--color-text-primary);
  
  &:hover:not(.btn-disabled) {
    background-color: var(--color-divider);
  }
}

// 按钮尺寸样式
.btn-sm {
  padding: var(--spacing-xs) var(--spacing-sm);
  font-size: 12px;
  border-radius: var(--border-radius-sm);
}

.btn-md {
  padding: var(--spacing-sm) var(--spacing-md);
  font-size: 14px;
}

.btn-lg {
  padding: var(--spacing-md) var(--spacing-lg);
  font-size: 16px;
  border-radius: var(--border-radius-lg);
}

// 按钮状态样式
.btn-disabled {
  opacity: 0.6;
  cursor: not-allowed;
  
  &:active {
    transform: none;
  }
}

.btn-loading {
  cursor: not-allowed;
  
  &:active {
    transform: none;
  }
}

.btn-block {
  width: 100%;
  display: flex;
}

.btn-round {
  border-radius: 50px;
}

// 图标和文本样式
.btn-icon {
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-text {
  white-space: nowrap;
}

.btn-loading-icon {
  display: flex;
  align-items: center;
  justify-content: center;
}

.loading-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: currentColor;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
</style>